package cn.com.api.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.api.entity.*;
import cn.com.api.mapper.ApiCarMapper;
import cn.com.api.mapper.ApiDingDanMapper;
import cn.com.api.mapper.ApiT_log_userMapper;
import cn.com.api.mapper.ApiT_log_user_detailMapper;
import cn.com.api.mapper.ApiT_productsMapper;
import cn.com.api.resultpage.CarOrderPage;
import cn.com.api.service.ApiT_log_userService;
import cn.com.util.*;

@Service
public class ApiT_log_userServiceImpl implements ApiT_log_userService{
	
	@Autowired
	private ApiT_log_userMapper apiT_log_userMapper;
	@Autowired
	private ApiT_productsMapper apiT_productsMapper;
	@Autowired
	private ApiDingDanMapper apiDingDanMapper;
	@Autowired
	private ApiCarMapper apiCarMapper;
	@Autowired
	private ApiT_log_user_detailMapper apiT_log_user_detailMapper;
	
	/** 每天商品访问次数记录 */
	@Transactional
	@Override
	public void updateGoodsVisit(String product_id) {
		//根据product_id 和当天日期 先查询是否存在记录，存在，则更新
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String date = dateFormat.format(new Date());
		Integer type = 0;//商品
		
		Long productid = Long.valueOf(product_id);
		
		ApiT_log_user logUser = apiT_log_userMapper.findApiT_log_user(productid, date,type);
		ApiT_products product = apiT_productsMapper.findT_productsById(productid);
		
		/** 记录详细信息 */
		ApiT_log_user_detail userDetail = new ApiT_log_user_detail();
		userDetail.setProduct_id(productid);
		userDetail.setType(1);//1-商品浏览
		userDetail.setCity_id(product.getCity_id());
		userDetail.setCreate_time(new Date());
		apiT_log_user_detailMapper.insertApiT_log_user_detail(userDetail);
		
		/** 记录当天汇总信息 */
		if(logUser!=null){
			Integer visitCount = logUser.getVisitcount()+1;
			logUser.setVisitcount(visitCount);
			apiT_log_userMapper.updateApiT_log_user(logUser);
		}else{
			logUser = new ApiT_log_user();
			logUser.setDate(new Date());
			logUser.setProduct_id(productid);
			logUser.setVisitcount(1);
			logUser.setOrdercount(0);
			logUser.setPurchase(0);
			
			logUser.setCity_id(product.getCity_id());
			logUser.setSumprice(BigDecimal.ZERO);
			logUser.setType(type);
			apiT_log_userMapper.insertApiT_log_user(logUser);
		}
	}
	
	/** 商品的下单量  购买数量   销售额 统计 */
	@Transactional
	@Override
	public void updateGoodsOrderInfo(String orderId) {
		// 查询该订单的商品信息   1-根据订单号获取订单信息  2-再获取订单的详细信息
		ApiDingDan dingDan = apiDingDanMapper.searchDingDanByTitleId(orderId);
		if(dingDan!=null){
			String[] cidArrayStr = dingDan.getCar_ids().split(",");
			List<Integer> cidList = new ArrayList<Integer>();
			for (String cidStr : cidArrayStr) {
				Integer cid = Integer.parseInt(cidStr);
				cidList.add(cid);
			}
			List<CarOrderPage> carList = apiCarMapper.searchCarGoodsByCids(cidList);
			if(carList!=null && carList.size()>0){
				SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
				String date = dateFormat.format(new Date());
				Integer type = 0;//商品
				for(CarOrderPage product: carList){
					Long product_id = product.getProduct_id();
					ApiT_log_user logUser = apiT_log_userMapper.findApiT_log_user(product_id, date,type);
					/** 记录详细信息 */
					ApiT_log_user_detail userDetail = new ApiT_log_user_detail();
					userDetail.setProduct_id(product_id);
					userDetail.setType(3);//3-下单
					userDetail.setOrder_id(orderId);
					userDetail.setPurchase(product.getCount());
					userDetail.setCity_id(product.getCity_id());
					userDetail.setCreate_time(new Date());
					apiT_log_user_detailMapper.insertApiT_log_user_detail(userDetail);
					
					/** 记录当天汇总信息 */
					if(logUser!=null){//更新  订单量 购买数量 销售额
						Integer orderCount = logUser.getOrdercount()+1;
						Integer purchase = logUser.getPurchase()+product.getCount();
					
						BigDecimal sumPrice = logUser.getSumprice().add(product.getPrice().multiply(Util.getBigDecimal(product.getCount().toString())));
						logUser.setOrdercount(orderCount);
						logUser.setPurchase(purchase);
						logUser.setSumprice(sumPrice);
						apiT_log_userMapper.updateApiT_log_user(logUser);
					}else{
						logUser = new ApiT_log_user();
						
						Integer orderCount = 1;
						Integer purchase = product.getCount();
						BigDecimal sumPrice = product.getPrice().multiply(Util.getBigDecimal(product.getCount().toString()));
						
						logUser.setDate(new Date());
						logUser.setProduct_id(product_id);
						logUser.setVisitcount(0);
						logUser.setOrdercount(orderCount);
						logUser.setPurchase(purchase);
						logUser.setCity_id(product.getCity_id());
						logUser.setSumprice(sumPrice);
						logUser.setType(type);
						apiT_log_userMapper.insertApiT_log_user(logUser);
					}
				}
			}
		}
	}

	@Override
	public void bannerClickStat(String bid, String cityid) {
		Long bannerid = Long.valueOf(bid);
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String date = dateFormat.format(new Date());
		ApiT_log_user logUser = apiT_log_userMapper.findApiT_log_user(bannerid, date,1);
		/** 记录详细信息 */
		ApiT_log_user_detail userDetail = new ApiT_log_user_detail();
		userDetail.setProduct_id(bannerid);
		userDetail.setType(2);//2-轮播
		userDetail.setCity_id(Integer.valueOf(cityid));
		userDetail.setCreate_time(new Date());
		apiT_log_user_detailMapper.insertApiT_log_user_detail(userDetail);
		
		/** 记录当天汇总信息 */
		if(logUser!=null){
			Integer visitCount = logUser.getVisitcount()+1;
			logUser.setVisitcount(visitCount);
			apiT_log_userMapper.updateApiT_log_user(logUser);
		}else{
			logUser = new ApiT_log_user();
			logUser.setDate(new Date());
			logUser.setProduct_id(bannerid);
			logUser.setVisitcount(1);
			logUser.setOrdercount(0);
			logUser.setPurchase(0);
			logUser.setCity_id(Integer.parseInt(cityid));
			logUser.setSumprice(BigDecimal.ZERO);
			logUser.setType(1);
			apiT_log_userMapper.insertApiT_log_user(logUser);
		}
	}
	
	/** 最近7天的访问量--网站的总汇总 */
	@Override
	public void get7DayVisitData(TimeSeries timeSeries) {
		timeSeries.add(new Day(1, 1, 2010), 50);  
        timeSeries.add(new Day(2, 1, 2010), 47);  
        timeSeries.add(new Day(3, 1, 2010), 82);  
        timeSeries.add(new Day(4, 1, 2010), 95);  
        timeSeries.add(new Day(5, 1, 2010), 104);  
        timeSeries.add(new Day(6, 1, 2010), 425);
        
        //从数据库查询每天的访问量--最近7天的日期
        
	}
	
	/** 根据日期查询商品的排行榜 */
	public List<Goods_Sale_List> getGoodsRankByDate(Map<String, Object> conditions,
			PageInfo pageInfo) {
		List<Goods_Sale_List> goodsList = apiT_log_userMapper.getGoodsRankByDateListPage(conditions, pageInfo);
		if(goodsList!=null && goodsList.size()>0){
			Integer startCount = (pageInfo.getCurrentPage()-1)*pageInfo.getShowCount();
			Integer i = 0;
			for(Goods_Sale_List goods : goodsList){
				goods.setRank_id((long) (startCount+i));
				i = i+1;
			}
			
		}
		return goodsList;
	}

	@Override
	public List<String> getYearList() {
		List<String> yearList = new ArrayList<String>();
		yearList = apiT_log_userMapper.getYearList();
		return yearList;
	}

	@Override
	public List<String> getMonthList(String year) {
		List<String> monthList = new ArrayList<String>();
		monthList = apiT_log_userMapper.getMonthList(year);
		return monthList;
	}

	@Override
	public List<String> getDayList(String year, String month) {
		List<String> dayList = new ArrayList<String>();
		dayList = apiT_log_userMapper.getDayList(year, month);
		return dayList;
	}
}
